# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=regex
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"  "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2019.08.19
pkgrel=1
pkgdesc="Alternative python regular expression module. (mingw-w64)"
arch=('any')
url='https://www.regex.org/'
license=('Python')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
options=('staticlibs' 'strip' '!debug')
source=("https://files.pythonhosted.org/packages/source/r/${_realname}/${_realname}-${pkgver}.tar.gz")
sha256sums=('587b62d48ca359d2d4f02d486f1f0aa9a20fbaf23a9d4198c4bed72ab2f6c849')

prepare() {
  cd "${srcdir}"
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
}

build() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

check() {
  for pver in {2,3}; do
    msg "Python ${pver} test for ${CARCH}"
    cd ${srcdir}/python${pver}-build-${CARCH}/build/lib.mingw-${pver}*
    ${MINGW_PREFIX}/bin/python${pver} -c 'from regex.test.test_regex import test_main; test_main()'
  done
}

package_python3-regex() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  sed -n '1,/^$/p' ${srcdir}/${_realname}-${pkgver}/regex_2/regex/regex.py > ${srcdir}/python3-build-${CARCH}/LICENSE
  install -Dm644 ${srcdir}/python3-build-${CARCH}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"

  install -v -m755 -d "${pkgdir}${MINGW_PREFIX}/share/doc/python3-regex"
  install -v -m644 ./docs/Features.html "${pkgdir}${MINGW_PREFIX}/share/doc/python3-regex/"
  install -v -m644 ./docs/Features.rst "${pkgdir}${MINGW_PREFIX}/share/doc/python3-regex/"
  install -v -m644 ./docs/UnicodeProperties.txt "${pkgdir}${MINGW_PREFIX}/share/doc/python3-regex/"
}

package_python2-regex() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  sed -n '1,/^$/p' ${srcdir}/${_realname}-${pkgver}/regex_2/regex/regex.py > ${srcdir}/python2-build-${CARCH}/LICENSE
  install -Dm644 ${srcdir}/python2-build-${CARCH}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"

  install -v -m755 -d "${pkgdir}${MINGW_PREFIX}/share/doc/python2-regex"
  install -v -m644 ./docs/Features.html "${pkgdir}${MINGW_PREFIX}/share/doc/python2-regex/"
  install -v -m644 ./docs/Features.rst "${pkgdir}${MINGW_PREFIX}/share/doc/python2-regex/"
  install -v -m644 ./docs/UnicodeProperties.txt "${pkgdir}${MINGW_PREFIX}/share/doc/python2-regex/"
}

package_mingw-w64-i686-python2-regex() {
  package_python2-regex
}

package_mingw-w64-i686-python3-regex() {
  package_python3-regex
}

package_mingw-w64-x86_64-python2-regex() {
  package_python2-regex
}

package_mingw-w64-x86_64-python3-regex() {
  package_python3-regex
}
